package com.example.month11.appupdate;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.month11.appupdate.updater.AppUpdater;
import com.example.month11.appupdate.updater.bean.AppUtils;
import com.example.month11.appupdate.updater.bean.DownloadBean;
import com.example.month11.appupdate.updater.net.INetCallBack;
import com.example.month11.appupdate.updater.net.INetDownloadCallBack;
import com.example.month11.appupdate.updater.ui.UpdateVersionShowDialog;
import com.example.recyclerview01.R;

import java.io.File;

/**
 * url: http://59.110.162.30/app_updater_version.json
 * response = {"title":“4.5.@更新啦!中“,
 * "content";"1，优化了阅读体验;\n2，上线了 hyman 的课程;\n3，修复了一些已知问题。“,
 * "url":"http://59,110,162,30/v450_imooc_updater.apk",
 * "md5":"14480fc08932105d55b9217c6d2fb90b",
 * "versionCode":“450"}
 */
public class AppUpdateActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_update);

        Button btn_update = findViewById(R.id.btn_update);
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadBean bean = new DownloadBean("aaa", "bbb", "ccc", "ddd", "eee");
                UpdateVersionShowDialog.show(AppUpdateActivity.this, bean);

                String url = "http://59.110.162.30/app_updater_version.json";
                /*AppUpdater.getInstance().getNetManager().get(url, new INetCallBack() {
                    @Override
                    public void success(String response) {
                        DownloadBean bean = DownloadBean.parseJson(response);
                        if (bean == null) {
                            return; // Toast 下载失败
                        }
                        try {
                            long versionCode = Long.parseLong(bean.versionCode);
                            if (versionCode <= AppUtils.getVersionCode(AppUpdateActivity.this)) {
                                // 不需要更新
                                return;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            return;
                        }
                        // 弹框。
                        UpdateVersionShowDialog.show(AppUpdateActivity.this, bean);
                        // downLoad(bean.url);
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        // Toast
                        throwable.printStackTrace();
                    }
                }, AppUpdateActivity.this);*/
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppUpdater.getInstance().getNetManager().cancel(this);
    }
}
